﻿/***********************************************************************
 * <copyright file="FrmXtraReportList.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   LinhMC
 * Email:    linhmc@buca.vn
 * Website:
 * Create Date: Wednesday, March 05, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;
using WindowsForms.AOnline.Model.BusinessObjects.Report;
using WindowsForms.AOnline.Presenter.Dictionary;
using WindowsForms.AOnline.Presenter.Report;
using WindowsForms.AOnline.Report.ReportClass;
using WindowsForms.AOnline.Session;
using WindowsForms.AOnline.View.Dictionary;
using WindowsForms.AOnline.View.Report;
using DevExpress.XtraEditors;

namespace WindowsForms.AOnline.Report.MainReport
{
    /// <summary>
    /// FrmXtraReportList
    /// </summary>
    public partial class FrmXtraReportList : XtraForm, IReportView, IReportGroupView//, ICurrenciesView
    {
        /// <summary>
        /// The _report list presenter
        /// </summary>
        private readonly ReportListPresenter _reportListPresenter;

        /// <summary>
        /// The _report group presenter
        /// </summary>
        private readonly ReportGroupPresenter _reportGroupPresenter;

        /// <summary>
        /// The _report helper
        /// </summary>
        private readonly ReportHelper _reportHelper;

        /// <summary>
        /// The _currencies presenter
        /// </summary>
      //  private readonly CurrenciesPresenter _currenciesPresenter;

        /// <summary>
        /// Gets or sets the common variable.
        /// </summary>
        /// <value>
        /// The common variable.
        /// </value>
        public GlobalVariable CommonVariable { get; set; }

        /// <summary>
        /// The _report list
        /// </summary>
        private List<ReportListModel> _reportList;
        private XtraReport xtraReport;
        /// <summary>
        /// Initializes a new instance of the <see cref="FrmXtraReportList"/> class.
        /// </summary>
        public FrmXtraReportList()
        {
            InitializeComponent();
            _reportGroupPresenter = new ReportGroupPresenter(this);
            _reportListPresenter = new ReportListPresenter(this);
        //    _currenciesPresenter = new CurrenciesPresenter(this);
            _reportHelper = new ReportHelper();
            CommonVariable = new GlobalVariable();
        }

        /// <summary>
        /// Loads the data.
        /// </summary>
        public void LoadData()
        {
            try
            {
                //_reportGroupPresenter.GetAllReportGroup();
                _reportList = _reportListPresenter.GetAllReportList();
           //     _currenciesPresenter.DisplayActive();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }

        }

        /// <summary>
        /// Views the report.
        /// </summary>
        private void ViewReport()
        {
            try
            {
                var reportID = gridReportDetailView.GetFocusedRowCellValue("ReportID").ToString();
         //       CommonVariable.FromDate = DateTime.Now;
        //        CommonVariable.ToDate = DateTime.Now;
                ReportTool.CommonVariable = CommonVariable;
                ReportTool.PrintPreview(this, _reportList, reportID, false, xtraReport);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// Loads the report group grid layout.
        /// </summary>
        private void LoadReportGroupGridLayout()
        {
            try
            {
                grdReportGroup.ForceInitialize();
                if (gridReportGroupView.RowCount <= 0)
                {
                    return;
                }
                gridReportGroupView.Columns["ReportGroupID"].Visible = false;
                gridReportGroupView.Columns["Description"].Visible = false;
                gridReportGroupView.Columns["IsVoucher"].Visible = false;
                gridReportGroupView.Columns["IsActive"].Visible = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// Loads the report detail grid layout.
        /// </summary>
        private void LoadReportDetailGridLayout()
        {
            try
            {
                if (_reportList.Count <= 0) return;
                for (int i = 0; i < gridReportDetailView.Columns.Count; i++)
                {
                    if (gridReportDetailView.Columns[i].FieldName != "ReportName")
                    {
                        gridReportDetailView.Columns[i].Visible = false;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        #region IReportView Members

        /// <summary>
        /// Gets or sets the report lists.
        /// </summary>
        /// <value>
        /// The report lists.
        /// </value>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<ReportListModel> ReportLists
        {
            set { grdReportDetail.DataSource = value.FindAll(item => item.ReportGroupId == (int)gridReportGroupView.GetFocusedRowCellValue("ReportGroupID")); }
        }

        /// <summary>
        /// Gets the report helper.
        /// </summary>
        /// <value>
        /// The report helper.
        /// </value>
        public ReportHelper ReportHelper
        {
            get { return _reportHelper; }
        }

        #endregion

        /// <summary>
        /// Handles the Load event of the FrmXtraReportList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void FrmXtraReportList_Load(object sender, EventArgs e)
        {
            LoadData();
            LoadReportGroupGridLayout();
            LoadReportDetailGridLayout();
        }

        /// <summary>
        /// Handles the Click event of the btnOk control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            ViewReport();
        }

        /// <summary>
        /// Handles the DoubleClick event of the gridReportDetailView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void gridReportDetailView_DoubleClick(object sender, EventArgs e)
        {
            ViewReport();
        }

        #region ICurrenciesView Members

        /// <summary>
        /// Sets the currencies.
        /// </summary>
        /// <value>
        /// The currencies.
        /// </value>
        //public IList<WindowsForms.AOnline.Model.BusinessObjects.Dictionary.CurrencyModel> Currencies
        //{
        //    set
        //    {
        //        grdLookUpCurrency.Properties.DataSource = value;
        //        grdLookUpCurrencyView.PopulateColumns(value);
        //        grdLookUpCurrencyView.Columns["CurrencyId"].Visible = false;
        //        grdLookUpCurrencyView.Columns["Prefix"].Visible = false;
        //        grdLookUpCurrencyView.Columns["Suffix"].Visible = false;
        //        grdLookUpCurrencyView.Columns["IsMain"].Visible = false;
        //        grdLookUpCurrencyView.Columns["IsActive"].Visible = false;
        //        grdLookUpCurrencyView.Columns["CurrencyCode"].Caption = "Mã tiền tệ";
        //        grdLookUpCurrencyView.Columns["CurrencyName"].Caption = "Tên tiền tệ";
        //        grdLookUpCurrency.Properties.DisplayMember = "CurrencyCode";
        //        grdLookUpCurrency.Properties.ValueMember = "CurrencyCode";
        //    }
        //}

        #endregion

        #region IReportGroupView Members

        /// <summary>
        /// Sets the report groups.
        /// </summary>
        /// <value>
        /// The report groups.
        /// </value>
        public List<ReportGroupModel> ReportGroups
        {
            set { grdReportGroup.DataSource = value; }
        }

        #endregion

        /// <summary>
        /// Handles the FocusedRowChanged event of the gridReportGroupView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs"/> instance containing the event data.</param>
        private void gridReportGroupView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            var reportGroupId = (int)gridReportGroupView.GetFocusedRowCellValue("ReportGroupID");
            if (_reportList == null) return;
            ReportLists = _reportList.FindAll(item => item.ReportGroupId == reportGroupId);
        }
    }
}